package com.example.tianling.admin.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.admin.service.ArticleData;
import com.example.tianling.admin.utils.DateUtil;
import com.example.tianling.admin.vo.TodayDataVo;
import com.example.tianling.common.vo.TrainArticleVo;
import com.example.tianling.common.vo.article;
import com.example.tianling.dao.ArticleMapper;
import com.example.tianling.dao.RoomMapper;
import com.example.tianling.entity.ArticleEntity;
import com.example.tianling.entity.RoomEntity;
import com.example.tianling.entity.UserEntity;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Service
public class ArticleDataServiceImpl
        extends ServiceImpl<ArticleMapper, ArticleEntity>
        implements ArticleData {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private RoomMapper roomMapper;
//加载刷新论坛页面
    public TodayDataVo load(){
        Date start = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 00:00:00");
        Date end = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 23:59:59");
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("create_time", start, end);
        Long type1Count = articleMapper.selectCount(queryWrapper.eq("type", 1));
        queryWrapper.clear();
        queryWrapper.between("create_time", start, end);
        Long type2Count = articleMapper.selectCount(queryWrapper.eq("type", 2));
        queryWrapper.clear();
        queryWrapper.between("create_time", start, end);
        Long type3Count = articleMapper.selectCount(queryWrapper.eq("type", 3));
        queryWrapper.clear();
        queryWrapper.between("create_time", start, end);
        Long totalCount = articleMapper.selectCount(queryWrapper.in("type", 1, 2, 3));
        TodayDataVo todayDataVo = new TodayDataVo(type1Count,type2Count,type3Count,totalCount);
        return todayDataVo;
    }

    public List loadArticleIndate(String startTime,String endTime){
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DATE(create_time) AS date",
                "SUM(CASE WHEN type = 1 THEN 1 ELSE 0 END) AS type_1_count",
                "SUM(CASE WHEN type = 2 THEN 1 ELSE 0 END) AS type_2_count",
                "SUM(CASE WHEN type = 3 THEN 1 ELSE 0 END) AS type_3_count",
                "SUM(CASE WHEN type IN (1, 2, 3) THEN 1 ELSE 0 END) AS total");
        queryWrapper.between("create_time", startTime, endTime);
        queryWrapper.groupBy("DATE(create_time)");
        queryWrapper.orderByDesc("DATE(create_time)");
        List<Map<String, Object>> result = articleMapper.selectMaps(queryWrapper);
        return result;
    }
    public IPage<article> searchAuthorArticle(Integer pageNum, Integer typeNum, String keyword){
        Page<article> pg = new Page<>(pageNum, 10);
        IPage<article> articleIPage = articleMapper.selectJoinPage(pg,article.class,
                new MPJLambdaWrapper<ArticleEntity>()
                        .selectAll(ArticleEntity.class)
                        .select(UserEntity::getUserName)
                        .select(UserEntity::getUserImg)
                        .eq(ArticleEntity::getType,typeNum)
                        .like(UserEntity::getUserName,keyword)
                        .leftJoin(UserEntity.class,UserEntity::getUserId,ArticleEntity::getUserId)
                        .orderByDesc(ArticleEntity::getCreateTime));
        return articleIPage;
    }
    public  IPage<TrainArticleVo> searchAuthorTrain(Integer page, Integer type, String keyword){
        Page<TrainArticleVo> pg = new Page<>(page, 10);
        IPage<TrainArticleVo> articleIPage = articleMapper.selectJoinPage(pg, TrainArticleVo.class,
                new MPJLambdaWrapper<ArticleEntity>()
                        .select(ArticleEntity::getContent)
                        .select(ArticleEntity::getArticleId)
                        .select(ArticleEntity::getLikeNum)
                        .select(ArticleEntity::getSaveNum)
                        .select(ArticleEntity::getViewNum)
                        .isNotNull(ArticleEntity::getRoomId)
                        .select(UserEntity::getUserName)
                        .select(UserEntity::getUserImg)
                        .like(UserEntity::getUserName,keyword)
                        .selectAll(RoomEntity.class)
                        .eq(ArticleEntity::getType,type)
                        .leftJoin(UserEntity.class,UserEntity::getUserId,ArticleEntity::getUserId)
                        .leftJoin(RoomEntity.class,RoomEntity::getRoomId,ArticleEntity::getRoomId)
                        .orderByDesc(ArticleEntity::getCreateTime));
        return articleIPage;
    }
    public Integer deleteArticle(List deleteList){
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("article_id",deleteList);
        queryWrapper.select("room_id");
        queryWrapper.isNotNull("room_id");
        List<Object> roomIdList = articleMapper.selectObjs(queryWrapper);
        List<String> list2 = (List<String>)(List) roomIdList;
        if(list2.size()>0){
            QueryWrapper<RoomEntity> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("room_id",list2);
            roomMapper.delete(queryWrapper1);
        }
        queryWrapper.clear();
        queryWrapper.in("article_id",deleteList);
        Integer result = articleMapper.delete(queryWrapper);
        return result;
    }
}
